# -*- coding: utf-8 -*

__author__ = 'huangxiaoli'

'''
    fabric 批量管理工具
'''
from fabric.api import *


env.hosts = [
    'root@192.168.1.200:22',
    'root@192.168.1.222:22'
]
env.passwords = {
    'root@192.168.1.200:22': "jsptpd123",
    'root@192.168.1.222:22': "jsptpd123"
}
env.always_use_pty = False

clone_path='https://huangxiaoli:fqxyhdsy@gitee.com/huangxiaoli/Stat.git'

@parallel(pool_size=5)
def gitclone():
    print("***git clone project***")
    #判断是否存在/home/developmetT
    if int(run("[ -e '/home/development' ] && echo 11 || echo 10")) == 11:
        print('***exist development***')
        #是否存在Stat项目
        if int(run("[ -e '/home/development/Stat' ] && echo 11 || echo 10")) == 11:
            print('***exist Stat***')
            print('***Do Nothing***')
        else:
            print('***none exist Stat***')
            print('***git clone Stat start***')
            #git clone Stat项目
            with cd('/home/development/'):
                run('git clone '+clone_path)
    else:
        print('none exist development')
        #新建development
        with cd('/home/'):
            run('mkdir development')
            #git clone Stat项目
            with cd('/home/development/'):
                run('git clone '+clone_path)

@parallel(pool_size=5)
def gitpull():
    print("***git pull project***")
    with cd('/home/development/Stat/'):
        run('git pull')

@parallel(pool_size=5)
def pip():
    print("***install pip***")
    run('yum -y install epel-release')
    run('yum -y install python-pip')
    print("***pip install redis***")
    run('pip install redis')

@parallel(pool_size=5)
def start():
    print("***start service***")
    with cd('/home/development/Stat/'):
        run('chmod +x moni_start.sh')
        run('nohup ./moni_start.sh >& /dev/null < /dev/null &')

@parallel(pool_size=5)
def stop():
    print("***stop service***")
    run("ps -ef |grep moni_ | awk '{print $2}' | xargs kill -9")

@parallel(pool_size=5)
def restart():
    print("***restart service***")
    stop()
    start()

@parallel(pool_size=5)
def status():
    print("check service status")
    run('ps -ef |grep moni_redis')






